<template>
	<view class="container">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true"><block v-slot:content>绑定账户</block></cu-custom>
		<view class="contents-white">
			<form>
				<view class="cu-form-group margin-top-xs">
					<view class="title text-black">账户名</view>
					<input v-model="fromData.username" placeholder="账户名" name="input" />
				</view>
				<view class="cu-form-group">
					<view class="title text-black">账户密码</view>
					<input v-model="fromData.password" placeholder="账户密码" name="input" />
				</view>
				<view class="padding-lr ">
					<view class="padding-sm flex flex-direction">
						<button class="cu-btn lg round bg-green text-white text-size-28" @click="onBindPhone">立即绑定</button>
						<button class="margin-top cu-btn lg round  bg-gradual-blue text-white text-size-28" @click="onRegister">注册账户</button>
					</view>
				</view>
			</form>
		</view>
	</view>
</template>

<script setup lang="ts">
import cuCustom from '@/components/cu-custom.vue';
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { useSettingStore } from '@/stores/index';
import { bindAccount } from '@/common/api/user';
const setting = useSettingStore();
const countdown = ref(0);
const fromData = ref({
	password: '',
	type: 0,
	username: ''
});
const getCode = (e: any) => {
	countdown.value = 60;
	let timer = setInterval(() => {
		if (countdown.value > 0) {
			countdown.value--;
		} else {
			clearInterval(timer);
		}
	}, 1000);
};
const onRegister = () => {
	uni.navigateTo({
		url: '/pages/manage/facilitator/create'
	});
};
/**
 * 绑定用户
 */
const onBindPhone = async () => {
	const res: any = await bindAccount(fromData.value);
	if (res.code == 1000) {
		setting.userType = fromData.value.type;
		uni.switchTab({
			url: '/pages/index/index'
		});
	} else {
		uni.showModal({
			title: '提示',
			content: '绑定账户失败,是否立即注册新账户?',
			confirmText: '立即注册',
			cancelText: '返回',
			success: async res => {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages/manage/facilitator/create'
					});
				} else if (res.cancel) {
					uni.navigateBack();
				}
			}
		});
	}
};
onLoad((option: any) => {
	fromData.value.type = option.type;
});
</script>

<style></style>
